package com.bootdo.quota.task;

import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.common.utils.ExceptionUtils;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.quota.dao.QuotaDao;
import com.bootdo.quota.domain.QuotaDO;
import com.bootdo.quota.domain.QuotaHistoryDO;
import com.bootdo.quota.dao.QuotaHistoryDao;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Desc
 * @date 2020/2/25
 *
 */
@Component
public class QuotaTask {

    private static final Logger logger = LoggerFactory.getLogger(QuotaTask.class);

    @Autowired
    private QuotaDao quotaMapper;

    @Autowired
    private QuotaHistoryDao quotaHistoryMapper;

    private volatile static QuotaTask uniqueInstance;

    private QuotaTask() {
    }

    public static QuotaTask getUniqueInstance() {
        synchronized(QuotaTask.class){
            if(uniqueInstance == null){
                uniqueInstance = new QuotaTask();
                System.out.println(Thread.currentThread().getName() + ": uniqueInstance is initalized...");
            } else {
                System.err.println(Thread.currentThread().getName()+"对象已存在"+uniqueInstance);
            }
        }
        return uniqueInstance;
    }

    /**
     * 新增额度
     * @param userId
     * @param money
     */
    public void updateMoney(Long userId, double money, String reason, String orderId, int changeType) {
        try {
            synchronized (QuotaTask.class){
                if (quotaMapper==null) {
                    quotaMapper = ApplicationContextRegister.getBean(QuotaDao.class);
                }

                if (quotaHistoryMapper==null) {
                    quotaHistoryMapper = ApplicationContextRegister.getBean(QuotaHistoryDao.class);
                }

                System.err.println(uniqueInstance+orderId);

                QuotaDO quota = quotaMapper.get(userId);
                double available = quota.getActualQuota();
                //新的业务规则相当于修改主表额度
                Long id = quota.getId();
                quota = new QuotaDO();
                quota.setId(id);
                if (StringUtils.isBlank(orderId)){
                    quota.setActualQuota(money);
                } else {
                    quota.setActualQuota(changeType == 1 ? available + money : available - money);
                }
                quotaMapper.updateByInfo(quota);

                //就相当于往额度流水表增加记录
                QuotaHistoryDO historyDO = new QuotaHistoryDO();
                if (StringUtils.isNotBlank(orderId) && !"TC0001".equals(orderId)){
                    historyDO.setOrderNo(orderId);
                }
                historyDO.setUserId(userId);
                historyDO.setQuota(available);
                if (StringUtils.isBlank(orderId)){
                    historyDO.setActualQuota(money);
                } else {
                    historyDO.setActualQuota(changeType == 1 ? available + money : available - money);
                }
                historyDO.setChangeReason(reason);
                historyDO.setStatus(2);
                historyDO.setChangeType(changeType);
                historyDO.setGmtDate(new Date());
                quotaHistoryMapper.saveBySelectMaxVersion(historyDO);
            }
        } catch (Exception e){
            logger.debug("=====修改流水=====>" + ExceptionUtils.getStackTraceAsString(e));
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
}
